<template>
  <div class="home">
    <!-- 头部 -->
    <row-vet></row-vet>
    <!-- 收货人地址列表 -->
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      add-button-text="新增地址"
      @add="onAdd"
      @edit="onEdit"
    />
 </div>
</template>

<script>
import header from '../util/header'
export default {
  data() {
    return {
      chosenAddressId: '',
      list: []
    }
  },
  components: {
    'row-vet': header
  },
  methods: {
    onAdd() {
      // 点击新增按钮,跳转
      // Toast('新增地址');
      this.$router.push("/address");
    },
    onEdit(item, index) {
      // 点击列表项编辑按钮,带数据返回添加收货人地址页面
      // localStorage.setItem('addressInfoEdit', JSON.stringify(item));
      this.$router.push({
        path: "/address",
        query: {
          id: item.id
        }
      });
    }
  },
  created() {
    // 渲染添加数据
    const val = JSON.parse(localStorage.getItem("addressInfo"));
    for(let i=0;i<val.length;i++){
      val[i].address = val[i].province + val[i].city + val[i].county + val[i].addressDetail    
    }
    this.list = val   
    if(val !==null){
      //进行循环
      this.list.forEach(item=>{
        //当为默认地址时候
        if(item.isDefault){
          //当前默认地址为当前id的地址
          this.chosenAddressId=item.id
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.title {
  background-color: #C73420;
  height: 1rem;
  .title_hm{
    line-height: 1rem;
    text-align: center;
    color: #ffffff;
    font-family: "微软雅黑";
    font-size: 0.5rem;
  }
}
</style>
